/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package df.servlet.interfacefile;

import df.bean.interfacefile.InterfaceExportGLBGHBean;
import df.bean.interfacefile.InterfaceExportGLBHNBean;
import df.bean.interfacefile.InterfaceExportGLBNHBean;
import df.bean.interfacefile.InterfaceExportGLBPHBean;
import df.bean.interfacefile.InterfaceExportGLBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author USER
 */
public class InterfaceExportGLSrvl extends HttpServlet {
   HttpSession session;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //response.setContentType("text/html;charset=UTF-8");
        session = request.getSession(true);
        response.setContentType("text/xml; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        String TYPE = request.getParameter("TYPE");
        String MM = request.getParameter("mm");
        String YYYY = request.getParameter("yyyy");
        String hospital_code = session.getAttribute("HOSPITAL_CODE").toString();
        InterfaceExportGLBean gl = new InterfaceExportGLBean(YYYY, MM, hospital_code);
        //add date 20100803
        InterfaceExportGLBPHBean gl_bph = new InterfaceExportGLBPHBean(YYYY, MM, hospital_code);
        InterfaceExportGLBGHBean gl_bgh = new InterfaceExportGLBGHBean(YYYY, MM, hospital_code);
        InterfaceExportGLBNHBean gl_bnh = new InterfaceExportGLBNHBean(YYYY, MM, hospital_code);
        InterfaceExportGLBHNBean gl_bhn = new InterfaceExportGLBHNBean(YYYY, MM, hospital_code);
        //--- 20100803
        String countdata = "0";
        try {
            if("AC".equalsIgnoreCase(TYPE)){
                //For Accu.
                //gl = new InterfaceExportGLBean(YYYY, MM, "AC",hospital_code);
            	//add date 20100803
            	if(hospital_code.equals("00001")){
                    countdata = gl_bgh.processAccu();            		
            	}else if(hospital_code.equals("00002")){
            		countdata = gl_bph.processAccu(); 
            	}else if(hospital_code.equals("00009")){
            		countdata = gl_bnh.processAccu();
            	}else if(hospital_code.equals("00029")){
            		countdata = gl_bhn.processAccu();
            	}else{}
            	//--- 20100803
                //countdata = gl.processAccu(); //comment 20100803
            }else{
                //For GL
                //gl = new InterfaceExportGLBean(YYYY, MM, "GL",hospital_code);
            	if(hospital_code.equals("00001")){
            		System.out.println("Process GL : BGH");
                    countdata = gl_bgh.processGlAccount();           		
            	}else if(hospital_code.equals("00002")){
            		System.out.println("Process GL : BPH");
            		countdata = gl_bph.processGlAccount();
            	}else if(hospital_code.equals("00009")){
            		System.out.println("Process GL : BNH");
            		countdata = gl_bnh.processGlAccount();
            	}else if(hospital_code.equals("00029")){
            		System.out.println("Process GL : BHN");
            		countdata = gl_bhn.processGlAccount();
            	}else{}
                //countdata = gl.processGlAccount(); //comment 20100803
            }
            try {
                Thread.sleep(20);
                out.print("<RESULT><SUCCESS>" + countdata + "</SUCCESS></RESULT>");
            }
            catch (Exception  e) {
                out.print("<RESULT><SUCCESS>Error</SUCCESS></RESULT>");
                e.printStackTrace(out);
            }
            finally {
                out.close();
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
